package ace.module.account.core.impl.controller.rest.admin.member;

import ace.cmp.core.model.PageResultDto;
import ace.cmp.core.model.R;
import ace.cmp.security.oauth2.resource.server.core.annotation.PreAuthorizeIsUserAuthenticated;
import ace.module.account.api.constant.AccountAuthorityConstant;
import ace.module.account.api.model.dto.IamAccountPassportDto;
import ace.module.account.api.model.input.passport.IamAccountPassportFilterInput;
import ace.module.account.api.model.input.passport.IamAccountPassportRemoveInput;
import ace.module.account.core.impl.service.member.MemberPassportService;
import ace.module.common.api.model.input.TenantInput;
import ace.module.common.api.model.input.TenantPageInput;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 会员通行证 admin controller.
 *
 * @author Caspar
 */
@Tag(name = "会员通行证-管理-API")
@PreAuthorizeIsUserAuthenticated
@AllArgsConstructor
@RestController
@RequestMapping(MemberPassportAdminController.PATH)
@Valid
public class MemberPassportAdminController {

  public static final String PATH = "/account/member-passport";
  public static final String PATH_ADMIN_TENANT_PATH = "/admin/tenant";
  public static final String PATH_ADMIN_TENANT_SEARCH = PATH_ADMIN_TENANT_PATH + "/search";
  public static final String PATH_ADMIN_TENANT_REMOVE = PATH_ADMIN_TENANT_PATH + "/remove";
  private final MemberPassportService memberPassportService;

  @Operation(summary = "搜索")
  @PreAuthorize("@securityService.hasAuthority('"
      + AccountAuthorityConstant.IAM_ACCOUNT_PASSPORT_ADMIN_READ + "')")
  @PostMapping(PATH_ADMIN_TENANT_SEARCH)
  public R<PageResultDto<IamAccountPassportDto>> search(
      @Valid @NotNull @RequestBody TenantPageInput<IamAccountPassportFilterInput> tenantPageInput) {
    PageResultDto<IamAccountPassportDto> result = memberPassportService.adminSearch(tenantPageInput);
    return R.ok(result);
  }


  @Operation(summary = "删除")
  @PreAuthorize("@securityService.hasAuthority('"
      + AccountAuthorityConstant.IAM_ACCOUNT_PASSPORT_ADMIN_REMOVE + "')")
  @PostMapping(PATH_ADMIN_TENANT_REMOVE)
  public R<Void> remove(
      @Valid @NotNull @RequestBody TenantInput<IamAccountPassportRemoveInput> input) {
    memberPassportService.adminRemove(input);
    return R.ok();
  }
}
